import React, { useState } from 'react';
import { register } from '../api/user';
import { message, Button, Form, Input } from 'antd';

const Register = () => {
    const [loading,setLoading] = useState(false);

    const onFinish = (values) => {
        setLoading(true)
        register({userName:values.userName,password:values.password})
        .then(res => {
            console.log(res)

            if(res.code === '200') {
                message.success(res.msg)
            }
        })
        .finally(() => {
            setLoading(false)
        })
    };

    return (
        <div>
            <Form
                layout='vertical'
                style={{
                    width:400,
                    margin:'50px auto'
                }}
                onFinish={onFinish}
            >
                <Form.Item
                    label="用户名"
                    name="userName"
                    rules={[
                        {
                            required: true,
                            message: '请输入用户名！',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="密码"
                    name="password"
                    rules={[
                        {
                            required: true,
                            message: '请输入密码！',
                        },
                    ]}
                >
                    <Input.Password />
                </Form.Item>

                <Form.Item
                    name="Confirm Password"
                    label="确认密码"
                    dependencies={['password']}
                    hasFeedback
                    rules={[
                    {
                        required: true,
                        message: '请再次输入密码！',
                    },
                    ({ getFieldValue }) => ({
                        validator(_, value) {
                        if (!value || getFieldValue('password') === value) {
                            return Promise.resolve();
                        }
                        return Promise.reject(new Error('两次输入不一致！'));
                        },
                    }),
                    ]}
                >
                    <Input.Password />
                </Form.Item>

                <Form.Item>
                    <Button type="primary" htmlType="submit" block shape='round' loading={loading}>
                        注册
                    </Button>
                </Form.Item>
            </Form>
        </div>
    )
}

export default Register;